package tianrun.ziguan.api.calculate.config.controller.asset;

import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tianrun.ziguan.api.calculate.config.config.exception.CommonErrorCode;
import tianrun.ziguan.api.calculate.config.dto.excel.TimeValueImport;
import tianrun.ziguan.api.calculate.config.dto.request.*;
import tianrun.ziguan.api.calculate.config.dto.response.AssetLabelDataBody;
import tianrun.ziguan.api.calculate.config.dto.response.AssetNameBody;
import tianrun.ziguan.api.calculate.config.entity.TCalAsset;
import tianrun.ziguan.api.calculate.config.service.cal.asset.ITCalAssetService;
import tianrun.ziguan.api.calculate.config.utils.HttpResponseTools;
import tianrun.ziguan.api.common.core.base.ResultBody;
import tianrun.ziguan.api.common.core.base.ResultPageBody;
import tianrun.ziguan.api.common.core.base.ReturnMessage;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cal/asset")
@Api(tags = "资产")
public class CalAssetController {

    private final ITCalAssetService calAssetService;

    public CalAssetController(ITCalAssetService calAssetService) {
        this.calAssetService = calAssetService;
    }

    @ApiOperation(value = "获取资产边缘端实时数据")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/labelData")
    public ResponseEntity<ResultPageBody<AssetLabelDataBody>> labelData(@RequestBody @Valid AssetDataParam param) {
        return HttpResponseTools.good(ResultPageBody.of(calAssetService.getLabelData(param), Function.identity()));
    }

    @ApiOperation(value = "(批量改名)获取资产列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/getAssetNameList")
    public ResponseEntity<ResultBody<List<AssetNameBody>>> getAssetNameList(@RequestBody @Valid AssetNameParam param) {
        return HttpResponseTools.good(ResultBody.of(calAssetService.getAssetNameList(param)));
    }
    @ApiOperation(value = "(批量改名)修改资产列表名称")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/update/batch")
    public ResponseEntity<ReturnMessage> getAssetNameList(@RequestBody @Valid List<TCalAssetParam> list)  {
        List<TCalAsset> assets = list.stream().map(t -> {
            TCalAsset asset = new TCalAsset();
            BeanUtils.copyProperties(t, asset);
            return asset;
        }).collect(Collectors.toList());
        calAssetService.saveOrUpdateBatchByGroup(assets);
        return HttpResponseTools.good();
    }
    @ApiOperation(value = "清理数据")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/clearData")
    public ResultBody<Boolean> clearData(@Valid ClearDataParam param) {
        return ResultBody.of(calAssetService.clearData(param));

    }
    @ApiOperation(value = "补录数据")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/addData")
    public ResponseEntity<ReturnMessage> addData(@Valid AddDataParam param, MultipartFile file) throws IOException {
        calAssetService.addData(param,file);
        return HttpResponseTools.good();
    }
    @ApiOperation(value = "补录数据模板下载")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/getAddDataTemplate")
    public void getAddDataTemplate(HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Connection", "keep-alive");
        response.setHeader("Keep-alive", "7200");
        response.setHeader("Pragma", "public");
        response.setHeader("X-Accel-Buffering", "no");
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("修正原始值-模板", "UTF-8") + ".xlsx");
        // 分页查询数据
        EasyExcel.write(response.getOutputStream(), TimeValueImport.class).sheet("模板").doWrite(Collections::emptyList);
    }

    @ApiOperation(value = "资产更换模板")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    @PostMapping("/update/template")
    public ResponseEntity<ReturnMessage> updateTemplate(@RequestBody UpdateTemplateParam param) {
        return calAssetService.updateTemplate(param)?HttpResponseTools.good():HttpResponseTools.bad(CommonErrorCode.SELECT_ERROR);
    }


}
